package com.quktzhuanka.app.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.quktzhuanka.app.CaiNiaoApplication;
import com.quktzhuanka.app.R;
import com.quktzhuanka.app.adapter.ShopMallGoodsRecyclerAdapter;
import com.quktzhuanka.app.base.BaseActivity;
import com.quktzhuanka.app.bean.GroupListBean;
import com.quktzhuanka.app.bean.Response;
import com.quktzhuanka.app.bean.ShopMallGoodsBean;
import com.quktzhuanka.app.bean.UserBean;
import com.quktzhuanka.app.bean.UserInfoBean;
import com.quktzhuanka.app.common.SPUtils;
import com.quktzhuanka.app.common.T;
import com.quktzhuanka.app.config.Constants;
import com.quktzhuanka.app.https.HttpUtils;
import com.quktzhuanka.app.https.onOKJsonHttpResponseHandler;
import com.quktzhuanka.app.my.PutForwardActivity;
import com.quktzhuanka.app.my.RechargeActivity;
import com.quktzhuanka.app.widget.CircleImageView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

public class VIPActivity extends BaseActivity {
    @BindView(R.id.civ_head)
    CircleImageView civHead;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.txt_huiyuan)
    TextView txtHuiyuan;
    @BindView(R.id.txt_code)
    TextView txtCode;
    @BindView(R.id.txt_jie)
    TextView txtJie;
    @BindView(R.id.txt_ye)
    TextView txtYe;
    @BindView(R.id.txt_may_money)
    TextView txtMayMoney;
    @BindView(R.id.txt_last_may)
    TextView txtLastMay;
    @BindView(R.id.txt_today_money)
    TextView txtTodayMoney;
    @BindView(R.id.txt_month_money)
    TextView txtMonthMoney;
    @BindView(R.id.txt_grade)
    TextView txtGrade;
    @BindView(R.id.pb_progressbar)
    ProgressBar pbProgressbar;
    @BindView(R.id.btn_copy)
    TextView btnCopy;
    @BindView(R.id.txt_name_group_one)
    TextView txtNameGroupOne;
    @BindView(R.id.txt_name_group_two)
    TextView txtNameGroupTwo;
    @BindView(R.id.txt_name_group_three)
    TextView txtNameGroupThree;
    @BindView(R.id.txt_name_group_four)
    TextView txtNameGroupFour;
    @BindView(R.id.txt_exp_group_one)
    TextView txtexpGroupOne;
    @BindView(R.id.txt_exp_group_two)
    TextView txtexpGroupTwo;
    @BindView(R.id.txt_exp_group_three)
    TextView txtexpGroupThree;
    @BindView(R.id.txt_exp_group_four)
    TextView txtexpGroupFour;
    @BindView(R.id.txt_tip)
    TextView txtTip;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
//    private View view;

    @BindView(R.id.radio_one)
    RadioButton radioOne;
    @BindView(R.id.radio_two)
    RadioButton radioTwo;
    @BindView(R.id.radio_three)
    RadioButton radioThree;
    @BindView(R.id.radio_four)
    RadioButton radioFour;


    @BindView(R.id.txt_sy1)
    TextView txtSy1;
    @BindView(R.id.txt_sy2)
    TextView txtSy2;
    @BindView(R.id.txt_sy3)
    TextView txtSy3;
    @BindView(R.id.txt_sy4)
    TextView txtSy4;
    @BindView(R.id.txt_sy5)
    TextView txtSy5;
    @BindView(R.id.txt_sy6)
    TextView txtSy6;

    @BindView(R.id.ll_12)
    LinearLayout ll12;
    WebView wv;
    List<GroupListBean.Item> radioitems;
    private ShopMallGoodsRecyclerAdapter shopRecyclerAdapter;

    List<ShopMallGoodsBean> taobaoGuesChildtBeans = new ArrayList<>();

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_vip);
        ButterKnife.bind(this);
        btnCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(txtCode.getText().toString().trim());
                T.showShort(VIPActivity.this, "复制成功，快去邀请好友吧");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        txtJie.setText("赚咖联盟已为您节省 " + SPUtils.getStringData(this, "my_money_one", "0.00") + " 元");
        RequestParams params = new RequestParams();
//        params.put("token", token);
        HttpUtils.post(Constants.GET_USER_MSG, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                // 输出错误信息
            }

            @Override
            public void onFinish() {
                // 隐藏进度条
                super.onFinish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    //返回码
                    UserInfoBean userBean = null;
                    int code = jsonObject.optInt("code");
                    //返回码说明
                    String msg = jsonObject.optString("msg");
                    String data = jsonObject.optString("data");
                    if (0 == code) {
                        if (!TextUtils.isEmpty(data)) {//GSON对数字都当做double解析
                            Gson gson = new Gson();
                            userBean = gson.fromJson(data.trim(), UserInfoBean.class);
                            CaiNiaoApplication.setUserInfoBean(userBean);
                        }
                        if (null != userBean) {
                            CaiNiaoApplication.setUserBean(new UserBean(userBean.user_detail.user_id, userBean.user_msg.group_id, SPUtils.getStringData(VIPActivity.this, "token", ""), userBean.user_detail.avatar, userBean.user_detail.nickname, userBean.user_msg.is_forever));
                            if (CaiNiaoApplication.getUserBean() != null) {
                                tvUsername.setText(CaiNiaoApplication.getUserBean().getName());
                                txtCode.setText(CaiNiaoApplication.getUserInfoBean().user_msg.auth_code);
                                Glide.with(VIPActivity.this).load(CaiNiaoApplication.getUserBean().getAvatar()).placeholder(R.mipmap.icon_defult_boy).error(R.mipmap.icon_defult_boy).dontAnimate().into(civHead);
                                if ("4".equals(CaiNiaoApplication.getUserBean().getGroup_id())) {
                                    ll12.setVisibility(View.GONE);
                                }else {
                                    ll12.setVisibility(View.VISIBLE);
                                }
//                                if ("3".equals(CaiNiaoApplication.getUserBean().getGroup_id())) {
//                                    ll12.setVisibility(View.GONE);
//                                } else if ("4".equals(CaiNiaoApplication.getUserBean().getGroup_id())) {
//                                    ll12.setVisibility(View.GONE);
//                                } else if ("2".equals(CaiNiaoApplication.getUserBean().getGroup_id())) {
//                                    ll12.setVisibility(View.VISIBLE);
//                                } else {
//                                    ll12.setVisibility(View.GONE);
//                                }
                                txtHuiyuan.setText(userBean.user_msg.group_name);
                            }
                        }
                    } else {
                        showToast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
                super.onStart();
            }
        });
        shopRecyclerAdapter = new ShopMallGoodsRecyclerAdapter(VIPActivity.this, R.layout.shop_mall_goods_item, taobaoGuesChildtBeans);
        recyclerView.setLayoutManager(new GridLayoutManager(VIPActivity.this,2));
        recyclerView.setAdapter(shopRecyclerAdapter);
        //点击进入详情
        shopRecyclerAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                ShopMallGoodsBean taobaoGuesChildtBean = taobaoGuesChildtBeans.get(position);
                if (taobaoGuesChildtBean != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("goods_id", taobaoGuesChildtBean.goods_id);
                    openActivity(MallGoodsDetailsActivity.class, bundle);
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        getList();
    }

    @Override
    protected void initData() {
        findViewById(R.id.img_feed).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(VIPActivity.this, FeedBackActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
            }
        });
        getGroupList();

    }
    private void getList(){
        RequestParams requestParams = new RequestParams();
        requestParams.put("cat_id", "39");
        HttpUtils.post(Constants.SLEF_MALL_GOODS, requestParams, new onOKJsonHttpResponseHandler<ShopMallGoodsBean>(new TypeToken<Response<ShopMallGoodsBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onSuccess(int statusCode, Response<ShopMallGoodsBean> datas) {
                if (datas.isSuccess()) {
                    List<ShopMallGoodsBean> list = datas.getData().list;
                    taobaoGuesChildtBeans.addAll(list);
                    Log.d("qwerqwewq",taobaoGuesChildtBeans.size()+"");
                    shopRecyclerAdapter.notifyDataSetChanged();
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }
    private void getGroupList() {
        RequestParams requestParams = new RequestParams();
        HttpUtils.post(Constants.GROUP_LIST, requestParams, new onOKJsonHttpResponseHandler<GroupListBean>(new TypeToken<Response<GroupListBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                showToast(throwable.getMessage());
            }

            @Override
            public void onSuccess(int statusCode, Response<GroupListBean> datas) {
                if (datas.isSuccess()) {
                    radioitems=datas.getData().list;
                    for (int i=0;i<radioitems.size();i++){
                        switch (i){
                            case 0:
                                txtNameGroupOne.setText(radioitems.get(i).title);
                                txtexpGroupOne.setText("0");
                                radioOne.setText(radioitems.get(i).title);
                                break;
                            case 1:
                                txtNameGroupTwo.setText(radioitems.get(i).title);
                                txtexpGroupTwo.setText(radioitems.get(i).exp);
                                radioTwo.setText(radioitems.get(i).title);
                                break;
                            case 2:
                                txtNameGroupThree.setText(radioitems.get(i).title);
                                pbProgressbar.setMax(Integer.valueOf(radioitems.get(i).exp));
                                txtexpGroupThree.setText(radioitems.get(i).exp);
                                radioThree.setText(radioitems.get(i).title);
                                break;
                            case 3:
                                txtNameGroupFour.setText(radioitems.get(i).title);
                                pbProgressbar.setMax(Integer.valueOf(radioitems.get(i).exp));
                                txtexpGroupFour.setText(radioitems.get(i).exp);
                                radioFour.setText(radioitems.get(i).title);
//                              txtTip.setText("——升级至"+items.get(i).title+"·4大权益——");
                                break;
                        }
                    }
                    getVipData();
                    radioOne.performClick( );
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }

    private void getVipData() {
        RequestParams params = new RequestParams();
        HttpUtils.post(Constants.GET_VIP_DATA, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                // 输出错误信息
            }

            @Override
            public void onFinish() {
                // 隐藏进度条
                super.onFinish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    //返回码
                    int code = jsonObject.optInt("code");
                    //返回码说明
                    String msg = jsonObject.optString("msg");
                    if (0 == code) {
                        txtYe.setText("余额: " + jsonObject.getJSONObject("data").getString("balance"));
                        txtGrade.setText(jsonObject.getJSONObject("data").getString("exp"));
                        pbProgressbar.setProgress(Integer.valueOf(jsonObject.getJSONObject("data").getString("exp")));
                        txtLastMay.setText(jsonObject.getJSONObject("data").getString("amount_last"));
                        txtMayMoney.setText(jsonObject.getJSONObject("data").getString("amount_current"));
                        txtMonthMoney.setText(jsonObject.getJSONObject("data").getString("amount_last_finish"));
                        txtTodayMoney.setText(jsonObject.getJSONObject("data").getString("amount_today"));
                        txtJie.setText("赚咖联盟已为您节省 " + jsonObject.getJSONObject("data").getString("amount") + " 元");
                        SPUtils.saveStringData(VIPActivity.this, "my_money_one", jsonObject.getJSONObject("data").getString("amount"));
                        SPUtils.saveStringData(VIPActivity.this, "my_money_two", jsonObject.getJSONObject("data").getString("amount_last"));
                        SPUtils.saveStringData(VIPActivity.this, "my_money_three", jsonObject.getJSONObject("data").getString("amount_current"));
                        SPUtils.saveStringData(VIPActivity.this, "my_money_four", jsonObject.getJSONObject("data").getString("balance"));

                    } else {
                        showToast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
                super.onStart();
            }
        });
    }
    @OnClick({R.id.btn_tx, R.id.txt_open,R.id.radio_one,R.id.radio_two,R.id.radio_three,R.id.radio_four})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_tx:
                if (CaiNiaoApplication.getUserInfoBean().user_msg.alipay_account == null) {
                    openActivity(BindActivity.class);
                } else {
                    Bundle b = new Bundle();
                    b.putString("balance", txtYe.getText().toString().replace("余额: ", ""));
                    openActivity(PutForwardActivity.class, b);
                }
                break;
            case R.id.txt_open:
                if("4".equals(CaiNiaoApplication.getUserBean().getGroup_id())){
                    T.showShort(VIPActivity.this, "您已经是终身VIP啦");
                    return;
                }
                openActivity(RechargeActivity.class);
                break;
            case R.id.radio_one:
                txtSy1.setText("收益提升"+radioitems.get(0).fee_user+"%");
                txtSy2.setText("收益提升"+radioitems.get(0).fee_user+"%");
                txtSy3.setText("奖励提升"+radioitems.get(0).referrer_rate+"%");
                txtSy4.setText("奖励提升"+radioitems.get(0).referrer_rate2+"%");
                txtSy5.setText("分红权益0%");
                txtSy6.setText("分红权益0%");
                break;
            case R.id.radio_two:
                txtSy1.setText("收益提升"+radioitems.get(1).fee_user+"%");
                txtSy2.setText("收益提升"+radioitems.get(1).fee_user+"%");
                txtSy3.setText("奖励提升"+radioitems.get(1).referrer_rate+"%");
                txtSy4.setText("奖励提升"+radioitems.get(1).referrer_rate2+"%");
                txtSy5.setText("分红权益0%");
                txtSy6.setText("分红权益0%");
                break;
            case R.id.radio_three:
                txtSy1.setText("收益提升"+radioitems.get(2).fee_user+"%");
                txtSy2.setText("收益提升"+radioitems.get(2).fee_user+"%");
                txtSy3.setText("奖励提升"+radioitems.get(2).referrer_rate+"%");
                txtSy4.setText("奖励提升"+radioitems.get(2).referrer_rate2+"%");
                txtSy5.setText("分红权益5-10%");
                txtSy6.setText("分红权益5%");
                break;
            case R.id.radio_four:
                txtSy1.setText("收益提升"+radioitems.get(3).fee_user+"%");
                txtSy2.setText("收益提升"+radioitems.get(3).fee_user+"%");
                txtSy3.setText("奖励提升"+radioitems.get(3).referrer_rate+"%");
                txtSy4.setText("奖励提升"+radioitems.get(3).referrer_rate2+"%");
                txtSy5.setText("分红权益5-12%");
                txtSy6.setText("分红权益5%");
                break;
        }
    }
    @Override
    protected void initListener() {

    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }

}
